<?php

namespace App\Http\Controllers\Sample;

use App\Helpers\Code;
use App\Models\Sample\SampleType;
use App\Models\Sample\SampleTypeWorkflow;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class TypeController extends Controller
{

    /**
     * @author lcy
     */
    public function __construct()
    {
        $this->_validators();
        $this->model = new SampleType();
    }

    /**
     * 添加样本类型
     * @param Request $request
     * @return array
     */
    public function addType(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $this->result['data'] = $this->model->addType($data);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑样本类型
     * @return array
     */
    public function editType(Request $request, $id)
    {
        try {
            $data = $request->only(['code', 'title', 'rank', 'is_judged', 'state']);
            $this->rules['state'] = ['required', 'in:0,1'];
            $this->rules['code'] = ['required', 'max:200', \Illuminate\Validation\Rule::unique('sample_types')->ignore($id)];
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editType($data, $id);
            $this->checked($model, [
                'null' => 'sample.type_not_exist',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取样本类型列表
     * @param Request $request
     * @return array
     */
    public function getTypeList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);
            $limit = (int)$request->input('limit');
            $sorter = $this->handleSort($request, $this->model->sorters);
            $defaultSorter = $request->input('sorter') ?? 'desc';
            return preprocess($this->model->getTypeList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取指定样本类型
     * @param $id
     * @return array
     */
    public function getType($id)
    {
        try {
            $model = $this->model->getType($id);
            $this->checked($model, ['null' => 'sample.type_not_exist',]);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取指定样本类型下工作流
     * @param $id
     * @return array
     */
    public function getTypeAll($id)
    {
        try {
            $this->model = new SampleTypeWorkflow();
            $model = $this->model->getTypeAll($id);
            $this->checked($model, ['null' => 'sample.type_not_exist',]);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'code' => 'bail|required|max:200|unique:sample_types',
            'title' => 'bail|required|max:200',
            'description' => 'max:200',
            'rank' => 'nullable|numeric',
            'is_judged' => 'bail|required|in:0,1',
        ];

        $this->messages = [
            'code.required' => trans('sample.code_required'),
            'code.max' => trans('sample.code_max'),
            'code.unique' => trans('sample.code_unique'),
            'title.required' => trans('sample.title_required'),
            'title.max' => trans('sample.title_max'),
            'description.max' => trans('sample.description_max'),
            'rank.numeric' => trans('sample.rank_numeric'),
            'is_judged.required' => trans('sample.is_judged_required'),
            'is_judged.in' => trans('sample.is_judged_in'),
            'state.required' => trans('sample.state_required'),
            'state.in' => trans('sample.state_in'),
        ];
    }
}
